package com.sportizz.sport;

import static com.sportizz.sport.CommonUtilities.SENDER_ID;
import static com.sportizz.sport.CommonUtilities.displayMessage;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder;
import android.util.Log;
import android.widget.RemoteViews;

import com.sportizz.sport.R;
import com.google.android.gcm.GCMBaseIntentService;

//

@SuppressLint({ "NewApi", "SimpleDateFormat" })
public class GCMIntentService extends GCMBaseIntentService {

	static int count = 0;

	public GCMIntentService() {
		super(SENDER_ID);

	}

	@Override
	protected void onRegistered(Context context, String registrationId) {
		Log.i("suraj", "Device registered: regId = " + registrationId);
		displayMessage(context, "Your device registred with GCM");
		ServerUtilities.register(context, MainActivity.name,
				MainActivity.email, MainActivity.device_id, registrationId);
	}

	@Override
	protected void onUnregistered(Context context, String registrationId) {
		Log.i("suraj", "Device unregistered");
		ServerUtilities.unregister(context, registrationId);
	}

	@Override
	protected void onMessage(Context context, Intent intent) {
		Log.i("suraj", "Received message");

		String message_cs = intent.getExtras().getString("cs");

		String message_game = intent.getExtras().getString("game");

		String message_match = intent.getExtras().getString("match");

		String start = intent.getExtras().getString("start");

		String player1 = intent.getExtras().getString("player1");
		String player2 = intent.getExtras().getString("player2");

		String player1_player2 = null;

		if (player1.equalsIgnoreCase("0")) {
			player1_player2 = null;
		} else {
			player1_player2 = player1 + " Vs " + player2;
		}

		String[] events;

		String date_str = MainActivity.getDate_informat(start);

		String message_date = "Starts at "
				+ MainActivity.getDate_informat(start);

		events = new String[6];
		events[0] = message_cs;
		events[1] = message_game;
		events[2] = message_match;
		events[3] = player1_player2;
		events[4] = message_date;
		events[5] = date_str;

		Log.v("suraj", "events=" + events[0] + "GAme = " + events[1]
				+ "Match = " + events[2] + "Date = " + events[5]);

		generateNotification(context, events);

	}

	@Override
	protected void onDeletedMessages(Context context, int total) {
		Log.i("suraj", "Received deleted messages notification");
	}

	@Override
	public void onError(Context context, String errorId) {
		Log.i("suraj", "Received error: " + errorId);
	}

	@Override
	protected boolean onRecoverableError(Context context, String errorId) {
		Log.i("suraj", "Received recoverable error: " + errorId);
		return super.onRecoverableError(context, errorId);
	}

	private static void generateNotification(Context context, String message[]) {
		int icon = R.drawable.ic_launcher;
		long when = System.currentTimeMillis();

		if (android.os.Build.VERSION.SDK_INT < 16) {

			Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
			Date currentLocalTime = cal.getTime();
			DateFormat date = new SimpleDateFormat("HH:mm a");
			date.setTimeZone(TimeZone.getDefault());
			String localTime = date.format(currentLocalTime);

			Log.v("suraj", "localTime =" + localTime);
			NotificationManager mNotificationManager = (NotificationManager) context
					.getSystemService(NOTIFICATION_SERVICE);

			@SuppressWarnings("deprecation")
			Notification notification = new Notification(icon, "Sportizza",
					when);

			RemoteViews contentView = null;

			if (message[3] == null) {
				contentView = new RemoteViews(context.getPackageName(),
						R.layout.custom_notification);
				contentView.setImageViewResource(R.id.image_notify,
						R.drawable.ic_launcher);
				contentView.setTextViewText(R.id.text_title, "Sportizza");
				contentView.setTextViewText(R.id.text1, message[0]);
				contentView.setTextViewText(R.id.text2, message[1]);
				contentView.setTextViewText(R.id.text3, message[4]);
				contentView.setTextViewText(R.id.text4, message[2]);
				contentView.setTextViewText(R.id.text5, null);
				contentView.setTextViewText(R.id.text_time, localTime);

			} else {
				contentView = new RemoteViews(context.getPackageName(),
						R.layout.custom_notification);
				contentView.setImageViewResource(R.id.image_notify,
						R.drawable.ic_launcher);
				contentView.setTextViewText(R.id.text_title, "Sportizza");
				contentView.setTextViewText(R.id.text1, message[0]);
				contentView.setTextViewText(R.id.text2, message[3]);
				contentView.setTextViewText(R.id.text3, message[4]);
				contentView.setTextViewText(R.id.text4, message[1]);
				contentView.setTextViewText(R.id.text5, message[2]);
				contentView.setTextViewText(R.id.text_time, localTime);
			}

			notification.contentView = contentView;

			Intent notificationIntent = new Intent(context, StartActivity.class);
			PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
					notificationIntent, 0);

			notification.contentIntent = contentIntent;

			notification.flags |= Notification.FLAG_AUTO_CANCEL; // clear the
																	// notification
			notification.defaults |= Notification.DEFAULT_VIBRATE; // Vibration
			notification.defaults |= Notification.DEFAULT_SOUND; // Sound

			mNotificationManager.notify(count, notification);

			count++;

		} else if (android.os.Build.VERSION.SDK_INT >= 16) {
			Intent intent = new Intent(context,
					com.sportizz.sport.StartActivity.class);
			PendingIntent pIntent = PendingIntent.getActivity(context, 0,
					intent, 0);

			Builder mBuilder = new NotificationCompat.Builder(context)
					.setAutoCancel(true)
					.setContentTitle("Sportizza")
					.setSmallIcon(R.drawable.ic_launcher)
					.setContentIntent(pIntent)
					.setDefaults(
							Notification.DEFAULT_LIGHTS
									| Notification.DEFAULT_VIBRATE
									| Notification.DEFAULT_SOUND);

			NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
			inboxStyle.setBigContentTitle("Sportizza");

			inboxStyle.addLine(message[0]);
			inboxStyle.addLine(message[1]);
			inboxStyle.addLine(message[2]);
			inboxStyle.addLine(message[3]);
			inboxStyle.addLine(message[4]);

			mBuilder.setStyle(inboxStyle);

			NotificationManager notificationManager = (NotificationManager) context
					.getSystemService(NOTIFICATION_SERVICE);

			notificationManager.notify(count, mBuilder.build());

			count++;

		}
	}

}
